package model;

import essenceClasses.CurrentTask;
import interfaceModel.ICurrentTaskModel;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Алексей
 * Date: 31.05.14
 * Time: 21:17
 * To change this template use File | Settings | File Templates.
 */
public class CurrentTaskModel implements ICurrentTaskModel {

    Connection conn = null;

    public CurrentTaskModel() {
        conn = Connector.getConnection();

    }


    @Override
    public boolean addCurrentTask(int taskID, int creatorID, String status, int recepientID) {
        return false;
    }

    @Override
    public List<CurrentTask> getCurrentTasksById(int taskID) throws SQLException {
        List<CurrentTask> list = new LinkedList<CurrentTask>();
        PreparedStatement preparedStatement = conn.prepareStatement(("SELECT * FROM Currenttask WHERE id = ?"));
        preparedStatement.setInt(1, taskID);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            //int id, String state, int recipiendId, int creatorId, int taskId
            list.add(new CurrentTask(resultSet.getInt("id"), resultSet.getString("state"), resultSet.getInt("recipient"),
                    resultSet.getInt("creator"), resultSet.getInt("idtaskdb")));
        }

        return list;
    }

    @Override
    public List<CurrentTask> getCurrentTasksByCreatorId(int creatorId) throws SQLException {
        List<CurrentTask> list = new LinkedList<CurrentTask>();
        PreparedStatement preparedStatement = conn.prepareStatement(("SELECT * FROM Currenttask WHERE creator = ?"));
        preparedStatement.setInt(1, creatorId);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            //int id, String state, int recipiendId, int creatorId, int taskId
            list.add(new CurrentTask(resultSet.getInt("id"), resultSet.getString("state"), resultSet.getInt("recipient"),
                    resultSet.getInt("creator"), resultSet.getInt("idtaskdb")));
        }

        return list;
    }


    @Override
    public List<CurrentTask> getCurrentTasksByRecepientId(int recepientId) throws SQLException {
        List<CurrentTask> list = new LinkedList<CurrentTask>();
        PreparedStatement preparedStatement = conn.prepareStatement(("SELECT * FROM Currenttask WHERE recipient = ?"));
        preparedStatement.setInt(1, recepientId);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            //int id, String state, int recipiendId, int creatorId, int taskId
            list.add(new CurrentTask(resultSet.getInt("id"), resultSet.getString("state"), resultSet.getInt("recipient"),
                    resultSet.getInt("creator"), resultSet.getInt("idtaskdb")));
        }

        return list;
    }

    @Override
    public boolean deleteCurrentTaskById(int id) throws SQLException {
        PreparedStatement preparedStatement = conn.prepareStatement(("SELECT * FROM Currenttask WHERE id = ?"));
        preparedStatement.executeUpdate("DELETE * FROM Currenttask WHERE id = " + id);
        return true;
    }

    @Override
    public boolean changeStateCurrentTaskById(int id, int state) throws SQLException {
        PreparedStatement preparedStatement = conn.prepareStatement(("UPDATE  Currenttask SET state=? WHERE id = ?"));
        preparedStatement.setInt(1, state);
        preparedStatement.setInt(2, id);
        preparedStatement.executeUpdate();
        return true;
    }

    public List<CurrentTask> getCurrentTaskByState(TaskState ts) throws SQLException {
        List<CurrentTask> list = new LinkedList<CurrentTask>();
        PreparedStatement preparedStatement = conn.prepareStatement(("SELECT * FROM Currenttask WHERE state = ?"));
        preparedStatement.setInt(1,ts.getCode());
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
                list.add(new CurrentTask(resultSet.getInt("id"), resultSet.getString("state"), resultSet.getInt("recipient"),
                        resultSet.getInt("creator"), resultSet.getInt("idtaskdb")));
        } return list;
    }


}
